package com.xu.cloud.common.feign;

import com.xu.cloud.common.constant.HttpConstant;
import com.xu.cloud.common.trace.TraceIdHelper;
import com.xu.cloud.common.util.HttpServletUtil;

import java.util.Map;
import java.util.Objects;

/**
 * 设置feign请求头
 *
 * @author xuguan
 * @since 2025/3/6
 */
public class FeignRequestHeadersInterceptor implements feign.RequestInterceptor {
    @Override
    public void apply(feign.RequestTemplate requestTemplate) {
        requestTemplate.header(HttpConstant.HEADER_TRACE_ID, TraceIdHelper.getTraceId());
        final Map<String, String> requestHeaders = HttpServletUtil.getRequestHeaders();
        for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
            final String headerName = entry.getKey();
            final String headerValue = entry.getValue();
            if (Objects.equals(headerName, HttpConstant.HEADER_ACCESS_TOKEN)
                    || Objects.equals(headerName, HttpConstant.HEADER_IDEMPOTENT_TOKEN)) {
                requestTemplate.header(headerName, headerValue);
            }
        }
    }
}
